package Thread;

import java.util.concurrent.*;

public class ThreadDemo32 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(4);
        //添加任务
        service.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello");
            }
        });
        PriorityBlockingQueue<Runnable> queue = new PriorityBlockingQueue<>(10);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,10,15, TimeUnit.SECONDS,queue);
        for (int i = 0; i < 8; i++) {
            final int taskNum = i;
            threadPoolExecutor.execute(() -> {
                System.out.println("任务 " + taskNum + " 开始，线程：" + Thread.currentThread().getName());
                try {
                    Thread.sleep(1000); // 模拟任务执行时间
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                System.out.println("任务 " + taskNum + " 完成，线程：" + Thread.currentThread().getName());
            });
        }
    }
}
